package com.holly.common_view.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.holly.common.utils.L;
import com.holly.common_view.R;

/**
 * 2016/11/25 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */
public class PageFlingView extends LinearLayout {
    private final String TAG = L.createTag(this);
    private static final int DEFAULT_TEXT_COLOR = Color.parseColor("#1976d2");
    private static final int DEFAULT_STROKE_COLOR = Color.parseColor("#1976d2");

    private Button mBtFirstPage;
    private Button mBtPreviousPage;
    private TextView mTvPage;
    private Button mBtNextPage;
    private Button mBtLastPage;

    private OnPageChangedListener mOnPageChangedListener;
    private int mPageCount = 0;
    private int mPageIndex = 0;

    public PageFlingView(Context context) {
        this(context, null);
    }

    public PageFlingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PageFlingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Common_View);
        int textColor = a.getColor(R.styleable.Common_View_labelTextColor, DEFAULT_TEXT_COLOR);
        int strokeColor = a.getColor(R.styleable.Common_View_strokeColor, DEFAULT_STROKE_COLOR);
        a.recycle();
        View view = inflate(getContext(), R.layout.view_page_fling, this);
        mBtFirstPage = (Button) view.findViewById(R.id.button_first_page);
        mBtPreviousPage = (Button) view.findViewById(R.id.button_previous_page);
        mBtNextPage = (Button) view.findViewById(R.id.button_next_page);
        mBtLastPage = (Button) view.findViewById(R.id.button_last_page);
        mTvPage = (TextView) view.findViewById(R.id.text_view_page);

        mTvPage.setTextColor(textColor);
        mBtFirstPage.setTextColor(textColor);
        mBtPreviousPage.setTextColor(textColor);
        mBtNextPage.setTextColor(textColor);
        mBtLastPage.setTextColor(textColor);

        GradientDrawable drawable;
        drawable = (GradientDrawable) mTvPage.getBackground();
        drawable.setStroke(1, strokeColor);
        drawable = (GradientDrawable) mBtFirstPage.getBackground();
        drawable.setStroke(1, strokeColor);
        drawable = (GradientDrawable) mBtPreviousPage.getBackground();
        drawable.setStroke(1, strokeColor);
        drawable = (GradientDrawable) mBtNextPage.getBackground();
        drawable.setStroke(1, strokeColor);
        drawable = (GradientDrawable) mBtLastPage.getBackground();
        drawable.setStroke(1, strokeColor);

        initListener();
    }

    private void initListener() {
        mBtFirstPage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageIndex > 1) {
                    int previous = mPageIndex;
                    mPageIndex = 1;
                    invalidatePageText();
                    if (mOnPageChangedListener != null) {
                        mOnPageChangedListener.onPageChanged(mPageIndex, previous, mPageCount);
                    }
                }
            }
        });

        mBtLastPage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageIndex < mPageCount) {
                    int previous = mPageIndex;
                    mPageIndex = mPageCount;
                    invalidatePageText();
                    if (mOnPageChangedListener != null) {
                        mOnPageChangedListener.onPageChanged(mPageIndex, previous, mPageCount);
                    }
                }
            }
        });

        mBtPreviousPage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageIndex > 1) {
                    int previous = mPageIndex;
                    mPageIndex = mPageIndex - 1;
                    invalidatePageText();
                    if (mOnPageChangedListener != null) {
                        mOnPageChangedListener.onPageChanged(mPageIndex, previous, mPageCount);
                    }
                }
            }
        });

        mBtNextPage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageIndex < mPageCount) {
                    int previous = mPageIndex;
                    mPageIndex = mPageIndex + 1;
                    invalidatePageText();
                    if (mOnPageChangedListener != null) {
                        mOnPageChangedListener.onPageChanged(mPageIndex, previous, mPageCount);
                    }
                }
            }
        });
    }

    public void reset(int pageCount) {
        reset(pageCount, pageCount >= 1 ? 1 : 0);
    }

    public void reset(int pageCount, int pageIndex) {
        if (pageCount < 0) {
            throw new IllegalArgumentException("pageCount can not < 0");
        }
        if (pageIndex < 0) {
            throw new IllegalArgumentException("pageIndex can not < 0");
        }
        if (pageIndex == 0 && pageCount > 0) {
            throw new IllegalArgumentException("when pageCount > 0,pageIndex can not = 0");
        }
        mPageCount = pageCount;
        mPageIndex = pageIndex;
        invalidatePageText();
    }

    private void invalidatePageText() {
        mTvPage.setText(mPageIndex + "/" + mPageCount);
    }

    public void setOnPageChangedListener(OnPageChangedListener onPageChangedListener) {
        mOnPageChangedListener = onPageChangedListener;
    }

    public int getPageIndex() {
        return mPageIndex;
    }

    public int getPageCount() {
        return mPageCount;
    }

    public static interface OnPageChangedListener {
        void onPageChanged(int nowPage, int previousPage, int pageCount);
    }
}
